layui.define(['form', 'table','element'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,element = layui.element;

  var accountId =$('input[name="id"]').val();

    element.on('tab(tab-info)', function(data){
        if(data.index == 0){
            table.reload('LAY-operate-list');
        }else if(data.index == 1){
            table.reload('LAY-withdraw-payee-list');
        }else if(data.index == 2){
            table.reload('LAY-deposit-list');
        }else if(data.index == 3){
            table.reload('LAY-income-list');
        }else if(data.index == 4){
            table.reload('LAY-pay-list');
        }else if(data.index == 5){
            table.reload('LAY-withdraw-list');
        }
    })

    table.render({
        elem: '#LAY-operate-list'
        ,url: layui.setter.webRoot + '/market/cloud/account/operate/list.data'
        ,where:{accountId:accountId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'id', width: 100, title: '流水号'}
            ,{field: 'moneyTypeName', title: '金额类型'}
            ,{field: 'typeName', title: '操作类型'}
            ,{field: 'moneyStr', title: '操作金额'}
            ,{field: 'operatorTypeName', title: '操作者类型'}
            ,{field: 'operatorName',title: '操作者名称'}
            ,{field: 'operateNote',title: '操作描述'}
            ,{field: 'createDate', title: '操作时间', sort: true}
        ]]
        ,page: false
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    table.render({
        elem: '#LAY-deposit-list'
        ,url: layui.setter.webRoot + '/market/cloud/account/order/deposit/list.data'
        ,where:{accountId:accountId}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'payTypeName', title: '充值方式'}
            ,{field:'payMoneyStr', title: '充值金额'}
            ,{field:'receiptMoneyStr', title: '实际到账'}
            ,{field:'payStatusName', title: '支付状态'}
            ,{field:'payDate', title: '支付时间'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-income-list'
        ,url: layui.setter.webRoot + '/market/cloud/account/order/list.data'
        ,where:{accountId:accountId,type:1}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'businessTradeNo', title: '业务订单号'}
            ,{field:'moneyStr', title: '支付金额'}
            ,{field:'accountId', title: '支付账号'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-pay-list'
        ,url: layui.setter.webRoot + '/market/cloud/account/order/list.data'
        ,where:{accountId:accountId,type:2}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'businessTradeNo', title: '业务订单号'}
            ,{field:'moneyStr', title: '支付金额'}
            ,{field:'payeeAccountId', title: '收款账号'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-withdraw-list'
        ,url: layui.setter.webRoot + '/market/cloud/account/order/withdraw/list.data'
        ,where:{accountId:accountId}
        ,method: 'post'
        ,parseData: function(res){
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,cols: [[
            {field: 'tradeNo', title: '订单号'}
            ,{field:'title', title: '订单标题'}
            ,{field:'moneyStr', title: '取款金额'}
            ,{field:'receivingTypeName', title: '收款人类型'}
            ,{field:'receivingId', title: '收款人ID'}
            ,{field:'receivingFaceImg', title: '收款人头像',templet:function(record){
                    if(record.receivingFaceImg) {
                        return '<image src="' + record.receivingFaceImg + '"/>';
                    }else{
                        return '无';
                    }
                }}
            ,{field:'receivingName', title: '收款人名称'}
            ,{field:'receivingPhone', title: '收款人电话'}
            ,{field:'stateName', title: '订单状态'}
            ,{field:'createDate', title: '创建时间'}
        ]]
        ,page: true
        ,limit: 20
        ,height: 'full-220'
    });

    table.render({
        elem: '#LAY-withdraw-payee-list'
        ,url: layui.setter.webRoot + '/market/cloud/account/withdraw/payee/list.data'
        ,where:{accountId:accountId}
        ,method: 'post'
        ,parseData: function (res) {
            return {
                "code": res.code,
                "msg": res.msg,
                "count": res.body.total,
                "data": res.body.data
            };
        }
        ,response: {statusCode: 200}
        ,size:'lg'
        ,cols: [[
            {field:'name', title: '取款真实姓名'}
            ,{field:'faceImg', title: '取款人头像',templet:function(record){
                    if(record.faceImg) {
                        return '<image src="' + record.faceImg + '"/>';
                    }else{
                        return '无';
                    }
                }}
            ,{field:'nickname', title: '取款昵称'}
            ,{field:'phone', title: '取款人手机号'}
            ,{field:'typeName', title: '类型'}
            ,{field:'appId', title: '取款应用ID'}
            ,{field:'openId', title: '取款用户ID'}
            ,{field:'updateDate', title: '最后修改'}
            ,{field:'createDate', title: '创建时间'}
            ,{title: '操作', width: 120, align:'center',fixed: 'right', toolbar: '#table-withdraw-payee-operate'}
        ]]
        ,page: true
        ,limit: 30
        ,height: 'full-220'
        ,text: '对不起，加载出现异常！'
    });

    //监听搜索
    form.on('submit(LAY-operate-search)', function(data){
        var field = data.field;
        //执行重载
        table.reload('LAY-operate-list', {
            where: field
            ,page: {curr:1}
        });
    });

    var active = {
        update: function () {
            layer.open({
                type: 2
                , title: '修改账号'
                , content: layui.setter.webRoot + '/market/cloud/account/update.html?id=' + accountId
                , maxmin: true
                , area: ['600px', '500px']
                , btn: ['确定', '取消']
                , yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/market/cloud/account/update.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.close(index); //关闭弹层
                                    layer.msg('修改成功',{icon: 1},function (){
                                        location.reload();
                                    });
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,recharge:function (){
            layer.open({
                type: 2
                , title: '充值账号'
                , content: layui.setter.webRoot + '/market/cloud/account/recharge.html?id=' + accountId
                , maxmin: true
                , area: ['600px', '500px']
                , btn: ['确定', '取消']
                , yes: function(index, layero){
                    var iframeWindow = window['layui-layer-iframe'+ index]
                        ,submitID = 'LAY-form-submit'
                        ,submit = layero.find('iframe').contents().find('#'+ submitID);
                    //监听提交
                    iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                        var field = data.field; //获取提交的字段
                        $.ajax({
                            type: "post",
                            url: layui.setter.webRoot + '/market/cloud/account/recharge.do',
                            data:field,
                            dataType:"json",
                            success:function(res){
                                if(res.code == 200){
                                    layer.close(index); //关闭弹层
                                    layer.msg('充值成功',{icon: 1},function (){
                                        location.reload();
                                    });
                                }else{
                                    layer.msg(res.msg,{icon: 2});
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
        ,delWithdrawPayee:function(obj){
            layer.confirm('确定要删除该取款用户吗？', function(index){
                $.ajax({
                    type: "post",
                    url: layui.setter.webRoot + '/market/cloud/account/withdrawPayee/del.do',
                    data:{id:obj.data.id},
                    dataType:"json",
                    success:function(res){
                        if(res.code == 200){
                            obj.del();
                        }else{
                            layer.msg(res.msg,{icon: 5});
                        }
                    }
                });
                layer.close(index);
            });
        }
    }

    $('.layui-btn.layui-btn-fun').on('click', function(){
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    table.on('tool(LAY-withdraw-payee-list)', function(obj){
        var type = obj.event ;
        active[type] ? active[type].call(this,obj) : '';
    });

  exports('cloud_account_info', {})
});